package com.woniu.zsd.platform.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.zsd.common.util.JWTUtil;
import com.woniu.zsd.common.util.Result;
import com.woniu.zsd.platform.annotation.RequirePermission;
import com.woniu.zsd.platform.annotation.RequireRole;
import com.woniu.zsd.platform.dto.AnnouncementDto;
import com.woniu.zsd.platform.form.AddAnnouncementForm;
import com.woniu.zsd.platform.param.AddAnnouncementParam;
import com.woniu.zsd.platform.service.AnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangtao
 * @since 2025年02月18日
 */
@RestController
@RequestMapping("/announcement")
@Api(tags = "公告类接口")
public class AnnouncementController {
    @Resource
    private AnnouncementService announcementService;
    @Resource
    private JWTUtil jwtUtil;

    //新增公告
    @PostMapping("/addAnnouncement")
    @ApiOperation("新增公告")
    @RequireRole({"超级管理员"})
    @RequirePermission("新增公告")
    public Result addAnnouncement(@RequestBody AddAnnouncementForm form,@RequestHeader("token") String token) throws Exception {
        Integer accountId = jwtUtil.getAccountId(token);
        announcementService.publishAnnouncement(BeanUtil.toBean(form, AddAnnouncementParam.class),accountId);

        return Result.success("新增公告成功");
    }

    //查询公告
    @GetMapping("/selectAnnouncement")
    @ApiOperation("查询公告")
    @RequireRole({"超级管理员"})
    @RequirePermission("查询公告")
    public Result selectAnnouncement() throws Exception {
        List<AnnouncementDto> announcementDto = announcementService.selectAnnouncement();
        return Result.success("查询公告成功",announcementDto);
    }

}

